package juc.thread.secuity;

import java.util.Hashtable;

/*
* 测试在多线程下的并发安全问题
* */
public class Test1 {
    private static int c=0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i <5000 ; i++) {
//                synchronized (Test1.class){
//                    c++;   //临界区:一段代码存在对共享资源的多线程读写操作
//                }
                c++;
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i <5000 ; i++) {
//                synchronized (Test1.class) {
//                    c--;//临界区:一段代码存在对共享资源的多线程读写操作
//                }
                c++;
            }
        }, "t2");
        t1.start();
        t2.start();
        //等待任务完成
        t1.join();
        t2.join();
        System.out.println(c);
    }
}
